package com.longqinuo.mycarservice.adapter;


import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

import com.longqinuo.mycarservice.R;
import com.longqinuo.mycarservice.entity.SecondCar;
import com.longqinuo.mycarservice.utils.PathUtils;

import java.util.List;

/**
 * Created by zxw on 2016/5/24.
 */
public class SecondCarAdapter extends RecyclerView.Adapter<SecondCarAdapter.MyViewHolder>{
    private List<SecondCar> mList;
    private int itemLayout;
    private OnItemClickListener onItemClickListener;
    private ImageLoader mImageLoader;
    public SecondCarAdapter(List<SecondCar> mList, int itemLayout,ImageLoader imageLoader) {
        this.mList = mList;
        this.itemLayout = itemLayout;
        this.mImageLoader = imageLoader;
    }
    public interface OnItemClickListener {
        void onItemClick(View itemView, int position);
    }
    public void setOnItemClickListener (OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(),itemLayout,null);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        SecondCar s = mList.get(position);

        String imgUrl = s.getImage();
        String imgs[] = imgUrl.split(",");
        String url = PathUtils.CAR_PHOTO+imgs[0];

        holder.image.setImageUrl(url,mImageLoader);
        holder.name.setText(s.getTitle());

        String date = s.getBuyDate();
        if (!TextUtils.isEmpty(date)) {
            String strs[] = date.split("-");
            date = strs[0]+"年";
        }
        String carState = s.getKm()+"万公里/"+date+"/个人";

        holder.state.setText(carState);
        holder.price.setText(s.getPrice()+"万");
        if (s.getTab()!=null) {
            holder.quality.setVisibility(View.VISIBLE);
        } else {
            holder.quality.setVisibility(View.GONE);
        }
        holder.quality.setVisibility(View.VISIBLE);

        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(View itemView) {
            super(itemView);
            this.image = (NetworkImageView) itemView.findViewById(R.id.niv_second_car_img);
            this.name = (TextView) itemView.findViewById(R.id.tv_second_car_name);
            this.state = (TextView) itemView.findViewById(R.id.tv_second_car_state);
            this.price = (TextView) itemView.findViewById(R.id.tv_second_car_price);
            this.quality = (TextView) itemView.findViewById(R.id.tv_quality_protect);
            this.isGood = (TextView) itemView.findViewById(R.id.tv_good_car_second);
        }
        private NetworkImageView image;
        private TextView name;
        private TextView state;
        private TextView price;
        private TextView quality;
        private TextView isGood;

    }
}

